package com.laker.autotest.utils;

import cn.hutool.core.text.csv.CsvData;
import cn.hutool.core.text.csv.CsvReadConfig;
import cn.hutool.core.text.csv.CsvRow;
import cn.hutool.core.text.csv.CsvUtil;
import lombok.extern.slf4j.Slf4j;

import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class EasyCsvUtil {

    /**
     * 读取csv文件转换为testdata
     *
     * @param csvPath eg:"data/baidu.csv"
     * @return
     */
    public static Object[][] getTestDataFromCsv(String csvPath) {
        List<Object[]> testData = new ArrayList<>();
        CsvData csvData = CsvUtil.getReader(CsvReadConfig.defaultConfig()
                        // 设置首行做为标题行
                        .setContainsHeader(true))
                .read(Paths.get(csvPath));
        log.info(">>>>>> csvPath  : {}", csvPath);
        log.info(">>>>>> csvHeader: {}", csvData.getHeader());
        List<CsvRow> rows = csvData.getRows();
        for (CsvRow csvRow : rows) {
            List<String> rowData = csvRow.getRawList();
            log.info(">>>>>> csvRowData: {}", rowData);
            // 将rowData作为一个数组添加到测试数据中
            testData.add(rowData.toArray());
        }
        return testData.toArray(new Object[0][0]);
    }
}
